<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: TranslatevarController.php 465 2012-02-12 05:33:23Z beimuaihui $
 */



class Multilang_TranslatevarController extends Baogg_Controller_Action {

	function init() {
	    Zend_Loader::loadClass ( 'TranslateVar' );
		$this->translateVar = new TranslateVar ( );
		
		Zend_Loader::loadClass ( 'Language' );
		$this->language = new Language ( );
		
		Zend_Loader::loadClass ( 'TranslateContent' );
        $this->translateContent = new TranslateContent ( );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;
		
		Baogg_Language::loadFile("translatecontent");
		Baogg_Language::loadFile("category");
		Baogg_Language::loadFile("menu");
		
		$params=$this->getRequest()->getParams();
		$form_url=$params;
		
		Zend_Loader::loadClass("Baogg_View_Extgrid");
		
		$cm=array();
		$cm['translate_var_id']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;
		$cm['translate_var_name']=array("sortable"=>true);
		$cm['translate_var_type2']=array("sortable"=>true,"header"=>BAOGG_LANGUAGE::get('translate_var_type'));
        $cm['menu_name']=array("sortable"=>true); #TODO add translate var category
        $cm['content']=array("sortable"=>true);
        $cm['translate_var_type']=array("hidden"=>true);
		
		
		$form_url['action']="data";
		$grid=new Baogg_View_Extgrid($form_url, $cm);
		$grid->setSelectionModel(true);
		$grid->setRowNumberer(true);
		$grid->addExistButtons(array('add','remove','query'),array('edit','delete'));
	
		$menu=new Menu();		
		$where=array("state"=>1);
		$rs_menu=$menu->getList($where,null,null,array("menu_id","menu_name","menu_pid"));
		
		$objTree=new Baogg_Db_Tree();
		$tr_menu=$objTree->rs2ComboTree($rs_menu);
		$arr_menu=$objTree->tr2ComboStore($tr_menu);
		

		
		
		$form_url['action']='add';
		$form=new Baogg_View_Extform(array("url"=>$form_url,"height"=>300,"width"=>600));		
		
		$form->addItem(0,array('name'=>'menu_id','xtype'=>'combo','store'=>$arr_menu));
		$form->addItem(0,array('name'=>'translate_var_name'));
		$form->addItem(1,array('name'=>'translate_var_type','xtype'=>'combo','store'=>Baogg_Language::array2store("arr_translate_var_type")));
		$form->addItem(2,array('name'=>'content'));
		$form->addItem(2,array('name'=>'translate_var_id',"xtype"=>"hidden","hideLabel"=>true));	
		$form->addExistButtons(array('-','save_grid','save_new','save','-','search','-','cancel'));//,'config','config'
		
		
		$this->view->form=$form;
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();
	}

	/**
	 * The data action
	 */
	function dataAction() {
	    $post=$this->_request->getPost ();
	    if(isset($post['translate_var_id'])){
    	    $post['translate_var_type']=$post['translate_var_type_id'];
    	    unset($post['translate_var_type_id']);
    	    unset($post['content']); #TODO this must remove after translate_content module finish
	    }
	    $where=array();$order=array();$limit=array();
	    
	   foreach((array)$post as $k=>$v){
	   	   
	       if($k=="start" || $k=="limit"){
	           $limit[$k]=$v;
	       }else if($k=="sort" || $k=="dir"){	       	   
	           $order[$k]=$v;
	       }else{
	       	   
	           if($k=="translate_var_id"  || $k=="translate_var_type" ){
	               if($v && is_numeric($v)){
	                   $v=(int)$v;
	                   $where[$k]=$v;
	               }
	           }else{
	               $v='%'.$v.'%';
	               $where[$k]=$v;
	           }
	           
	       }
	   }
	   
	   $rs=$this->translateVar->getList($where,$order,$limit);	
	   $arr_translate_var_type=BAOGG_LANGUAGE::get('arr_translate_var_type');   
	   foreach((array)$rs as $k=>$v)	   
	   {
	   		if($v['translate_var_type']==2)
	   		{
	   			$js_win="(function(){(new Ext.ux.Dialog({id:'translate_array_{$v['translate_var_id']}',url:'".BAOGG_BASE_URL."multilang/translatearray/edit/translate_var_pid/{$v['translate_var_id']}',width:800,height:500,modal:true})).show();return ;})();";
	   			$rs[$k]['action']='<a href="javascript:'.$js_win.'"><img src="'.BAOGG_THEME.'images/action/edit.png" title="'.BAOGG_LANGUAGE::get('translate_var_id').'" /></a>';        		   			
	   		}
	   		$rs[$k]['translate_var_type2']=$arr_translate_var_type[$v['translate_var_type']];
	   		
	   }
	   
	   $rs_count=$this->translateVar->getList($where);
	   
	  
	   echo  Zend_Json::encode(array("count"=>count($rs_count),"data"=>$rs));
	   
	   $this->_helper->viewRenderer->setNoRender ();
		
	}

	
	/**
	 * The add action
	 */
	function addAction(){
		$this->saveAction();
	}
	
	/**
	 * The edit action
	 */
	function editAction() {
		$this->saveAction();
	}

	/**
	 * The delete action
	 */
	function deleteAction() {
        
	    $ret=array("success"=>false,"msg"=>"delete_fail");
	    $id = Baogg_Controller_Action::_getParam ( 'ids' );
		 
		   
		
		if ( $this->translateContent->_delete ( $id ) && $this->translateVar->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}
		
		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();
	}
		
	function removeAction()
	{
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;
		
	    $ret=array("success"=>false,"msg"=>"save_fail");
	    
	    //check form
		if (!$this->_request->isPost ())
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;
		}
		
		$formData = $this->_request->getPost ();

		if(!$formData)
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;     
		}

		$formData['translate_var_type']=$formData['translate_var_type_id'];
		$arr=$this->translateVar->filterForm($formData);
		
        $arr_content=$this->translateContent->filterForm($formData);
        $arr_content['language_id']=isset($arr_content['language_id'])?$arr_content['language_id']:BAOGG_LANGUAGE_ID;
        
		$rs = $this->translateVar->getSame($arr);
		//echo '<pre>';print_r($rs);exit;
		if(($arr["translate_var_id"])>0){
			//update record
			//$permission_type = 2;
			//echo '<pre>';print_r($rs);var_dump(count($rs));exit;
			if( count($rs)==1 &&  $rs[0] == $arr["translate_var_id"] ){
			$this->translateVar->_update($arr,$arr["translate_var_id"]);
			    
			$rs_content=$this->translateContent->getSame($arr_content);
            if(count($rs_content)==1){
                    $this->translateContent->_update($arr_content,$rs_content[0]);
            }else{
            	   
                    $this->translateContent->_insert($arr_content); 
             }
            
			    $ret['success'] = true;				
				$ret['msg'] = "save_ok";			
			}else{
				$ret['msg'] = "exist_same_record";
			}
			
		  
			
		}else{
			//insert record
			//$permission_type = 1;			
			if($rs){				
				$ret['msg'] = "exist_same_record";
			}else{
			    unset($arr["translate_var_id"]);
			    $arr_content['translate_var_id']=$this->translateVar->_insert($arr);
				$ret['success'] = $arr_content['translate_var_id'] >0?true:false;
				$ret['success'] &= $this->translateContent->_insert($arr_content);
				$ret['msg'] = "add_ok";
			}
		}
				
		echo Baogg_Language::outputResult($ret);
		
		$this->_helper->viewRenderer->setNoRender ();
		
	}

}
